package vg.modules.navigator.components.popup_menu_component.popup_menu_items;

import com.google.common.collect.Lists;
import vg.interfaces.ll_services.data_base_service.data.graph.Graph;
import vg.interfaces.ll_services.data_base_service.data.graph.Vertex;
import vg.modules.navigator.components.SmartTreeComponent;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.main_manager.VGMainServiceHelper;
import vg.services.plugin_manager.event_and_request.request.UIRequestAddTab;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.util.Collection;
import java.util.List;

public class OpenVerticesPopupMenuItem implements IPopupMenuItem {
    // Constants
    //private static final Icon openVerticesIcon;

    static {
        //openVerticesIcon = new ImageIcon(FileUtils.getWorkingDir() + "data/resources/textures/navigator/vertex.png");
    }

    @Override
    public void action(Collection<JTree> trees, JTree lastSelectedTree) {
        List<TreePath> treePaths = Lists.newArrayList();
        for (JTree tree : trees) {
            TreePath[] paths = tree.getSelectionPaths();
            if (paths != null) {
                treePaths.addAll(Lists.newArrayList(paths));
            }
        }

        List<Vertex> vertices = Lists.newArrayList();
        for (TreePath path : treePaths) {
            SmartTreeComponent.SmartTreeNode smartTreeNode = (SmartTreeComponent.SmartTreeNode)path.getLastPathComponent();
            if (smartTreeNode.isVertexHeader()) {
                vertices.add(VGMainServiceHelper.graphDataBaseManager.getVertex(smartTreeNode.getVertexHeader().getId()));
            }
        }

        Graph graph = new Graph(vertices, null, true);
        AGraphView graphView = VGMainServiceHelper.graphViewManager.newGraphView(graph);
        UIRequestAddTab request = new UIRequestAddTab(graphView);
        VGMainServiceHelper.userInterfaceManager.sendRequest(request);
    }

    @Override
    public boolean isVisible(Collection<JTree> trees, JTree lastSelectedTree) {
        List<TreePath> treePaths = Lists.newArrayList();
        for (JTree tree : trees) {
            TreePath[] paths = tree.getSelectionPaths();
            if (paths != null) {
                treePaths.addAll(Lists.newArrayList(paths));
            }
        }

        for (TreePath path : treePaths) {
            SmartTreeComponent.SmartTreeNode smartTreeNode = (SmartTreeComponent.SmartTreeNode)path.getLastPathComponent();
            if (smartTreeNode.isVertexHeader()) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Icon getIcon() {
        return null;
    }

    @Override
    public String getActionName() {
        return "Open vertices";
    }
}
